import mqtt from 'mqtt';

const options = {
  keepalive: 30,
  clientId: `mqtt_client_${Math.random().toString(16).substring(2, 10)}`,
  clean: true,
  reconnectPeriod: 5000, // 断开后5秒重连
  connectTimeout: 30 * 1000, // 30秒连接超时
};

// 使用环境变量或配置文件中的地址
const brokerUrl = 'wss://penglink.pcwxcn.top:8084/mqtt'; // 请替换为实际的 MQTT broker 地址

const client = mqtt.connect(brokerUrl, options);

client.on('connect', () => {
  // console.log('Connected to MQTT broker');
});

client.on('error', (err) => {
  // console.error('Connection error: ', err);
});

client.on('offline', () => {
  // console.log('Client is offline');
});

client.on('reconnect', () => {
  // console.log('Client is trying to reconnect');
});

export default client;